package com.liumulingyu.configuration;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 作者：刘杰（QQ:770161055,WeChat:yjso0826）
 * 时间： 2018/7/16
 */
@Service
public class LoginInterceptor extends HandlerInterceptorAdapter {
    final Log logger = LogFactory.getLog(LoginInterceptor.class);
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        try {
            HttpSession session = request.getSession();
            logger.debug("session  : " + session.getAttribute("admin_id"));
            if (session.getAttribute("admin_id") == null) {
                response.sendRedirect("/index");
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
